import numpy as np

def eulcidean_distance(point1, point2):
    return np.linalg.norm(point1 - point2)

def own_eulcidean_distance(point1, point2):
    return np.sqrt(np.sum((point1 - point2) ** 2))
def cal_rectangle(points):
        # 计算边界框的最小和最大坐标
        min_x = min([x[0] for x in points])
        min_y = min([y[1] for y in points])
        max_x = max([x[0] for x in points])
        max_y = max([y[1] for y in points])
        return min_x, min_y, max_x, max_y
def all_eulidean_distance(points1_x, points2_x, points1_y, points2_y, min_x, min_y, max_x, max_y):
    points1_x = (points1_x - min_x) / (max_x - min_x)
    points2_x = (points2_x - min_x) / (max_x - min_x)
    points1_y = (points1_y - min_y) / (max_y - min_y)
    points2_y = (points2_y - min_y) / (max_y - min_y)
    return np.sqrt((points1_x - points2_x) ** 2 + (points1_y - points2_y) ** 2)

def process_points(points_image, point):
    x = point[0]
    y = point[1]
    points_image[y, x] = 1
def part_aspect_ratio(points, part_type):
    if part_type == "left_eye_landmarks":
        #垂直眼睛
        eye_top1 = np.array(points[2][:2])
        eye_down1 = np.array(points[12][:2])
        eye_top2 = np.array(points[3][:2])
        eye_down2 = np.array(points[10][:2])
        #水平眼睛
        eye_right = np.array(points[7][:2])
        eye_left = np.array(points[15][:2])
        A = own_eulcidean_distance(eye_top1, eye_down1)
        B = own_eulcidean_distance(eye_top2, eye_down2)
        C = own_eulcidean_distance(eye_right, eye_left)
        return (A + B) / (2.0 * C)
    elif part_type == "right_eye_landmarks":
        eye_top1 = np.array(points[3][:2])
        eye_down1 = np.array(points[13][:2])
        eye_top2 = np.array(points[5][:2])
        eye_down2 = np.array(points[11][:2])
        eye_right = np.array(points[0][:2])
        eye_left = np.array(points[8][:2])
        A = own_eulcidean_distance(eye_top1, eye_down1)
        B = own_eulcidean_distance(eye_top2, eye_down2)
        C = own_eulcidean_distance(eye_right, eye_left)
        return (A + B) / (2.0 * C)
    else:
        mouth_top1 = np.array(points[19][:2])
        mouth_down1 = np.array(points[11][:2])
        mouth_top2 = np.array(points[1][:2])
        mouth_down2 = np.array(points[9][:2])
        mouth_right = np.array(points[5][:2])
        mouth_left = np.array(points[15][:2])
        A = own_eulcidean_distance(mouth_top1, mouth_down1)
        B = own_eulcidean_distance(mouth_top2, mouth_down2)
        C = own_eulcidean_distance(mouth_right, mouth_left)
        return (A + B) / (2.0 * C)
def part_all_ratio(points, part_type):
    if part_type == "left_eye_landmarks":
        min_x, min_y, max_x, max_y = cal_rectangle(points)
        eye_tops = points[0: 7]
        eye_downs = points[14:7:-1]
        eye_tops_x = np.array([point[0] for point in eye_tops])
        eye_down_x = np.array([point[0] for point in eye_downs])
        eye_tops_y = np.array([point[1] for point in eye_tops])
        eye_down_y = np.array([point[1] for point in eye_downs])
        # eye_right = np.array(points[7][:2])
        # eye_left = np.array(points[15][:2])
        A = all_eulidean_distance(eye_tops_x, eye_down_x, eye_tops_y, eye_down_y, min_x, min_y, max_x, max_y)
        # B = own_eulcidean_distance(eye_right, eye_left)
        return A 
    elif part_type == "right_eye_landmarks":
        min_x, min_y, max_x, max_y = cal_rectangle(points)
        eye_tops = points[1: 8]
        eye_downs = points[:8:-1]
        eye_tops_x = np.array([point[0] for point in eye_tops])
        eye_down_x = np.array([point[0] for point in eye_downs])
        eye_tops_y = np.array([point[1] for point in eye_tops])
        eye_down_y = np.array([point[1] for point in eye_downs])
        eye_right = np.array(points[8][:2])
        eye_left = np.array(points[0][:2])
        # A = all_eulidean_distance(eye_tops_x, eye_down_x, eye_tops_y, eye_down_y)
        # B = own_eulcidean_distance(eye_right, eye_left)
        A = all_eulidean_distance(eye_tops_x, eye_down_x, eye_tops_y, eye_down_y, min_x, min_y, max_x, max_y)
        return A 
    elif part_type == 'mouth_landmarks':
        min_x, min_y, max_x, max_y = cal_rectangle(points)
        mouth_tops = points[16:20]
        mouth_tops.extend(points[0: 5])
        mouth_downs = points[14:5:-1]
        mouth_tops_x = np.array([point[0] for point in mouth_tops])
        mouth_down_x = np.array([point[0] for point in mouth_downs])
        mouth_tops_y = np.array([point[1] for point in mouth_tops])
        mouth_down_y = np.array([point[1] for point in mouth_downs])
        mouth_right = np.array(points[5][:2])
        mouth_left = np.array(points[15][:2])
        # A = all_eulidean_distance(mouth_tops_x, mouth_down_x, mouth_tops_y, mouth_down_y)
        A = all_eulidean_distance(mouth_tops_x, mouth_down_x, mouth_tops_y, mouth_down_y, min_x, min_y, max_x, max_y)
        # B = own_eulcidean_distance(mouth_right, mouth_left)
        return A
    else:
        return None

